package com.hngy.xpq.shoppingcartmodule.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hngy.xpq.shoppingcartmodule.R;
import com.hngy.xpq.shoppingcartmodule.assets.XpqIcoMoon;
import com.hngy.xpq.shoppingcartmodule.assets.XpqIcoMoonUtils;
import com.hngy.xpq.shoppingcartmodule.bean.Cart;
import com.hngy.xpq.shoppingcartmodule.bean.Shop;
import com.hngy.xpq.shoppingcartmodule.database.SqlCart;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {

    private List<Shop> listData;
    private int number = 1;
    TextView homeCartNumber;

    static class ViewHolder extends RecyclerView.ViewHolder {

        View fileView;
        SqlCart sqlCart;
        ImageView image;
        TextView name, price, dis, time, des;
        TextView number;  // EditText要符合设计，但只是为了使初学者明白
        Button add_cart, reduce, add;

        public ViewHolder(View itemView) {
            super(itemView);
            fileView = itemView;
            // TextView
            name = itemView.findViewById(R.id.homeName);
            price = itemView.findViewById(R.id.homePrice);
            dis = itemView.findViewById(R.id.homeDis);
            time = itemView.findViewById(R.id.homeTime);
            des = itemView.findViewById(R.id.homeDes);
            number = itemView.findViewById(R.id.homeNumber);
            // Button
            add_cart = itemView.findViewById(R.id.add_cart);
            reduce = itemView.findViewById(R.id.reduce);
            add = itemView.findViewById(R.id.add);
        }
    }

    // 获取到数据
    public ListAdapter(List<Shop> list, TextView number) {
        this.listData = list;
        this.homeCartNumber = number;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        // 加入购物车
        holder.add_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当前list
                int position = holder.getAdapterPosition();
                // 初始化数据库
                holder.sqlCart = new SqlCart(parent.getContext());
                // 数据 -> SqlCart
                Cart c = new Cart();
                c.setShop_id(listData.get(position).getId());
                c.setNumber(number);
                // 添加逻辑，如果存在了此商品，需要数量覆盖
                List<Cart> list = holder.sqlCart.select(parent, c.getShop_id());
                if (list.size() > 0) {
                    c.setId(list.get(0).getId());
                    c.setNumber(c.getNumber() + list.get(0).getNumber());
                    if (holder.sqlCart.update(c)) {   // 修改数据
                        // 更新购物车 红色提示 车内数量
                        int index = Integer.valueOf(homeCartNumber.getText().toString()) + Integer.valueOf(holder.number.getText().toString());
                        homeCartNumber.setText(String.valueOf(index));
                        Toast.makeText(holder.itemView.getContext(), "加入购物车成功!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(holder.itemView.getContext(), "加入购物车失败!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (holder.sqlCart.insert(c)) {   // 添加进入数据库
                        int index = Integer.valueOf(homeCartNumber.getText().toString()) + c.getNumber();
                        homeCartNumber.setText(String.valueOf(index));
                        Toast.makeText(holder.itemView.getContext(), "加入购物车成功!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(holder.itemView.getContext(), "加入购物车失败!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        // 添加数量
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {   // 防止输入非int类型数据
                    number = Integer.valueOf(holder.number.getText().toString());
                } catch (NumberFormatException e) {
                    Toast.makeText(holder.itemView.getContext(), "请输入正确的数量!", Toast.LENGTH_SHORT).show();
                    number = 1;
                    holder.number.setText(String.valueOf(number));
                }
                if (number > 100) {
                    Toast.makeText(holder.itemView.getContext(), "最多只可购买100份!", Toast.LENGTH_SHORT).show();
                } else {
                    number++;
                    holder.number.setText(String.valueOf(number));
                }
            }
        });
        // 减少数量
        holder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {   // 防止输入非int类型数据
                    number = Integer.valueOf(holder.number.getText().toString());
                } catch (NumberFormatException e) {
                    Toast.makeText(holder.itemView.getContext(), "请输入正确的数量!", Toast.LENGTH_SHORT).show();
                    number = 1;
                    holder.number.setText(String.valueOf(number));
                }
                if (number <= 1) {
                    Toast.makeText(holder.itemView.getContext(), "当前为最小值!", Toast.LENGTH_SHORT).show();
                } else {
                    number--;
                    holder.number.setText(String.valueOf(number));
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 数据绑定
        final Shop s = listData.get(position);
        holder.name.setText(s.getName());
        holder.price.setText("￥" + s.getPrice());
        holder.dis.setText(s.getDis() + "折");
        holder.time.setText(s.getTime());
        holder.des.setText(s.getDes());
        holder.add_cart.setText(XpqIcoMoon.FJ);
        XpqIcoMoonUtils.setIOC(holder.add_cart);
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

}
